import * as types from './mutation-types'
import { getData, getToken} from '../utils/axios'
import { ElMessage } from 'element-plus'
import router from '../router/index'
const actions ={
    async actionLogin ({commit},param) {//{commit}=context,原来的参数是context  context.commit context有三个参数{state,commit,getters}另外额外参数只能是一个值
        let res=await getData('busMem/busbase/login',param)
        console.log(res.data)
        if(res.data.flag){
            let data=res.data.data;
            
            commit(types.SETKEYNO,data.keyInfo.keySerialNo)
            commit(types.SETPUBILIC,data.keyInfo.publicKey)
            commit(types.SETBUSID,data.busbaseInfo.id)
            commit(types.SETADMINID,data.adminInfo.id)
            commit(types.SETSHOPNAME,data.busbaseInfo.shopName)
            commit(types.SETUSERNAME,data.adminInfo.userName)
           router.push('/introduce')
         }else{
             ElMessage.error(res.data.message);
         }
    },
    async decrement({commit}){
        let res=await getToken()
        let data=res.data.data;
        data.expires_in= Date.now() + data.expires_in * 1000;
        commit(types.SETTOKEN,data.access_token)
        commit(types.SETEXPIRES,data.expires_in)
        commit(types.SETKEYNO,data.keyInfo.keySerialNo)
        commit(types.SETPUBILIC,data.keyInfo.publicKey)
        console.log(router)
       
    } 
}

export default actions


